<?php

class diy_uiCompentModel extends model{
	public $table="diy_uicompent";
	
	public function __construct(&$base){
		parent::__construct($base);
	}
	public function getTpl($tpl){
		if(!ISWAP) return $tpl;
		$m=get("m")?get("m",'h'):"index";
		$a=get("a")?get("a","h"):"default";
		$page=M("uicompent_page")->selectRow(" m='".$m."' AND a='".$a."' ");
		if($page){
			return "diy.html";
		}
		return $tpl;
	}
	/**页面DIY**/
	public function getPage(){
		$m=get("m")?get("m",'h'):"index";
		$a=get("a")?get("a","h"):"default";
		$page=M("uicompent_page")->selectRow(" m='".$m."' AND a='".$a."' ");
		if($page){
			$this->get($page['tags']);
		}
	}
	/**uiCompent
	*uiCompent是一组静态数据	 分为wap、app、wxapp
	 * */
	public function get($tags){
		if(empty($tags)) return false;
		$arr=explode(",",$tags);
		$key="upg-".$tags;
		 
		$html='';
		 
		if(1==1 || !$html=cache()->get($key)){
			 
			foreach($arr as $a){
				 
				$row=$this->selectRow("  tag='".sql($a)."'");
				
				if(empty($row)) continue;
				
				if($row['typeid']==1){
					$html =$html.$row['content'];
				}else{
					
					$uc=M("uicompent")->selectRow("id=".$row['ucid']);
					$config=json_decode(base64_decode($row['jsondata']),true);
					$res=$this->getData($uc['funtype'],$config,$uc['tpl']);
					$html =$html.$res;
				}
				
			}
			cache()->set($key,$html,10);
			
		}
		echo $html;
		
	}
	//预览
	public function view($id){
			$row=$this->selectRow("  id='".$id."'");
			if(empty($row)) return false;
			
			if($row['typeid']==1){
				$html =$row['content'];
			}else{
				
				$uc=M("uicompent")->selectRow("id=".$row['ucid']);
				$config=json_decode(base64_decode($row['jsondata']),true);
				$res=$this->getData($uc['funtype'],$config,$uc['tpl']);
				$html =$res;
			}
			echo $html;
	}
	
	public function getData($funtype,$config,$tpl){
		switch($funtype){			
			case "pro-list":
				$mdDataList=$this->proSelect($config);
				break;
			case "art-list":
				$mdDataList=$this->artSelect($config);
				break;
			case "pic-list":
			 
				$mdDataList=$this->pictureSelect($config);
				break;
			 
			case "nav-wap":
				$mdDataList=$this->navSelect($config);
				break;
			case "tags":
				$mdDataList=$this->tagSelect($config);
				break;
			case "header":
				 
				$mdDataList=$this->headerSelect($config);
				break;
			case "footer":
				
				$mdDataList=$this->footerSelect($config);
				break;
			default:
				$mdDataList=array(
					0=>array(
						"title"=>"得推网",
						"imgurl"=>"http://img.deitui.com/?w=400&h=200&text=得推网"
					),
					1=>array(
						"title"=>"得推网",
						"imgurl"=>"http://img.deitui.com/?w=400&h=200&text=得推网"
					),
				);
				
				
				break;
		}
		
		ob_start();
		require $tpl;
		$content=ob_get_contents();
		ob_end_clean();
		return $content;
	}
	
	function proSelect($config){
		switch($config['funkey']){
			case "pro-new":
				$where="  status=2 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,price,lower_price,buy_num",
					"order"=>"last_time DESC"
				);
				
			case "pro-hot":
				$where="  status=2  AND ishot=1 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,price,lower_price,buy_num",
					"order"=>"last_time DESC"
				);
				
			case "pro-recommend":
				$where="  status=2  AND is_recommend=1 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,price,lower_price,buy_num",
					"order"=>"last_time DESC"
				);
				
				break;
		}		
		$data=M("product")->select($option);
		if($data){
			foreach($data as $k=>$v){
				$v['imgurl']=images_site($v['imgurl']);
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
	
	function artSelect($config){
		
		switch($config['funkey']){
			case "art-new":
				$where="  status=2 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,view_num,comment_num",
					"order"=>"last_time DESC"
				);
				
			case "art-hot":
				$where="  status=2  AND ishot=1 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,view_num,comment_num",
					"order"=>"last_time DESC"
				);
				
			case "art-recommend":
				$where="  status=2  AND is_recommend=1 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,view_num,comment_num",
					"order"=>"last_time DESC"
				);
				
				break;
		}		
		$data=M("article")->select($option);
		if($data){
			foreach($data as $k=>$v){
				$v['imgurl']=images_site($v['imgurl']);
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
	function pictureSelect($config){
		switch($config['funkey']){
			case "pic-new":
				$where="  status=2 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,view_num,comment_num,picnum",
					"order"=>"last_time DESC"
				);
				
			case "pic-hot":
				$where="  status=2  AND ishot=1 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,view_num,comment_num,picnum",
					"order"=>"last_time DESC"
				);
				
			case "pic-recommend":
				$where="  status=2  AND is_recommend=1 ";
				if($config['catid']){
					$cids=M("category")->id_family($config['catid']);
					$where.=" AND catid in("._implode($cids).") ";
				}
				$option=array(
					"where"=>$where,
					"limit"=>$config['limit'],
					"fields"=>"id,title,description,imgurl,view_num,comment_num,picnum",
					"order"=>"last_time DESC"
				);
				
				break;
		}		
		$data=M("picture")->select($option);
		if($data){
			foreach($data as $k=>$v){
				$v['imgurl']=images_site($v['imgurl']);
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
	public function navSelect($config){
		switch($config['funkey']){
			case "nav-wap":
				$data=M("navbar")->navlist(21);
				break;
		}
		return $data;
	}
	
	public function tagSelect($config){
		 
		switch($config['funkey']){
			case "tags-article":
				$data=M("article_tags")->select(array(
					"where"=>" shopid=".SHOPID ,
					"order"=>"total_num DESC",
					"fields"=>"tagid,title,total_num,view_num",
					"limit"=>$config['limit']
				));
				if($data){
					foreach($data as $k=>$v){
						$v['url']="/index.php?m=article&a=tag&tagid=".$v['tagid'];
						$data[$k]=$v;
					}
				}
				break;
			case "tags-picture":
				$data=M("picture_tags")->select(array(
					"where"=>" shopid=".SHOPID ,
					"order"=>"total_num DESC",
					"fields"=>"tagid,title,total_num,view_num",
					"limit"=>$config['limit']
				));
				if($data){
					foreach($data as $k=>$v){
						$v['url']="/index.php?m=picture&a=tag&tagid=".$v['tagid'];
						$data[$k]=$v;
					}
				}
				break;
			case "tags-product":
				$data=M("product_tags")->select(array(
					"where"=>" shopid=".SHOPID ,
					"order"=>"total_num DESC",
					"fields"=>"tagid,title,total_num,view_num",
					"limit"=>$config['limit']
				));
				if($data){
					foreach($data as $k=>$v){
						$v['url']="/index.php?m=product&a=tag&tagid=".$v['tagid'];
						$data[$k]=$v;
					}
				}
				break;
		}
		return $data;
	}
	
	public function headerSelect($config){
		 
		return $config;
	}
	
	public function footerSelect($config){
		$data=M("navbar")->navlist(2);
		return $data;
	}
	
}
?>